package study.dq.jms.ps;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @author dongqun
 * @version 1.0.0
 * @since 2021/04/20 14:54:00
 */
public class Subscriber implements MessageListener {
    public Subscriber(String topicName) {
        try {
            TopicConnectionFactory factory = new ActiveMQConnectionFactory("admin", "admin", "tcp://127.0.0.1:61616");
            TopicConnection connect = factory.createTopicConnection();
            connect.setClientID("topic_test_client_1");
            TopicSession subSession = connect.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
            Topic topic = subSession.createTopic(topicName);
            TopicSubscriber topicSubscriber = subSession.createDurableSubscriber(topic,"test_client_1", null, true);
            topicSubscriber.setMessageListener(this);
            connect.start();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMessage(Message message) {
        try {
            TextMessage msg = (TextMessage) message;
//            message.setJMSRedelivered(true);
//            msg.acknowledge();
//            System.out.println(message.getJMSPriority());
            System.out.println(msg.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Subscriber subscriber = new Subscriber("topic_test");
    }
}
